package com.yonyou.isv.demo.openapi.event.todo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Sets;
import com.yonyou.isv.demo.openapi.base.module.isv.event.ISVEventListener;
import com.yonyou.isv.demo.openapi.base.network.cryptor.EncryptionHolder;
import com.yonyou.isv.demo.openapi.event.ISVEventExtendDto;
import com.yonyou.isv.demo.openapi.event.IsvEventExtendType;
import com.yonyou.isv.demo.openapi.pojo.ThirdUserBo;
import com.yonyou.isv.demo.openapi.sso.SingleSignOn;
import com.yonyou.isv.demo.openapi.utils.CommonUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Set;

/**
 * @description: 部门新增事件
 * @author: nishch
 * @create: 2021-07-05
 **/
@Slf4j
@Component
@RequiredArgsConstructor
public class TodoAddEvent implements ISVEventListener<ISVEventExtendDto> {

    @Resource
    SingleSignOn singleSignOn;

    /**
     * TODO:在这里写待办的处理逻辑
     * @param type 事项类型
     * @param event 解析后对象
     * @param holder 响应消息体
     * @return boolean
     */
    @Override
    public boolean onEvent(String type, ISVEventExtendDto event, EncryptionHolder holder){
        String jsonString = JSON.toJSONString(event);
        if (StringUtils.isNotBlank(jsonString)){
            Map<String,String> info = JSONObject.parseObject(event.getContent(), Map.class);
            Map<String,Object> result = JSONObject.parseObject(info.get("value"), Map.class);
            log.info("todo id: {}", JSON.toJSONString(result));
            log.info("todo webUrl: {}", JSON.toJSONString(result.get("webUrl")));
            return true;
        }
        return false;
    }

    @Override
    public int priority() {
        return ISVEventListener.super.priority()-100;
    }

    @Override
    public Set<String> supportTypes() {
        return Sets.newHashSet(IsvEventExtendType.TODO_CENTER_ADD_TODO);
    }

    @Override
    public Class<ISVEventExtendDto> getEventClass() {
        return ISVEventExtendDto.class;
    }

}
